clear all

* Directories - chose right one
*global workspace "/Users/agnarfreyrhelgason/Dropbox/Projects/2020/Panama/ebal"
*global workspace "W:/Dropbox/Projects/2020/Panama/shared/DATA"

cd $workspace


*** France
use france_ebal.dta, clear 

egen polsop_top40 = group(pol_sophistication_binary top40), label
egen polsop_pr = group(pol_sophistication_binary pr_socialist), label
 * 1= 0 0
 * 2 = 0 1
 * 3= 1 0
 * 4= 1 1

 rename combinedproblem_econelite ecelite
 rename combinedproblem_citizens citizens
 
save france_ebal_temp.dta,replace

cap qui frame drop results
qui frame create results str20(sample type treat dv) est se 

foreach t of varlist treat_full treat_1month {

use france_ebal_temp.dta, clear 

qui reg  `t' female age tertiary retired
keep if e(sample)

ebalance `t' female age tertiary retired, tar(2) base(WEIGHT) wttreat maxiter(50) tolerance(0.03) keep("ebal_fr_`t'.dta") replace

svyset [pweight = _webal]
	
	foreach var of varlist v27r v51r v53r v54r v55r v57r v58r v59r ecelite citizens { 
	local i=1
		cap frame change default
		cap frame drop temp

		foreach sub of varlist polsop_top40 polsop_pr  {	
			cap frame drop temp
			
			eststo `t'_`var'_`i': svy: reg `var' `t'##i.`sub'
			local i=`i'+1
			margins, dydx(`t') over(`sub') post
			frame copy default temp
			frame change temp

				mat b=e(b)
				mat v=e(V)

				gen sample = "ISSP, France"
				gen type = "`sub', 0 both"
				gen treat = "`t'"
				gen dv = "`var'"
				gen est = b[1,5]
				local se = sqrt(v[5,5])
				gen se = `se'

				replace type = "`sub', 0 polsop, 1 other" if _n==2
				replace est = b[1,6] if _n==2
				local se = sqrt(v[6,6])
				replace se = `se' if _n==2

				replace type = "`sub', 1 polsop, 0 other" if _n==3
				replace est = b[1,7] if _n==3
				local se = sqrt(v[7,7])
				replace se = `se' if _n==3
				
				replace type = "`sub', 1 both" if _n==4
				replace est = b[1,8] if _n==4
				local se = sqrt(v[8,8])
				replace se = `se' if _n==4


				keep if _n==1 | _n==2 | _n==3 | _n==4
				keep sample type treat dv est se

				frame change results
				frameappend temp
				cap frame drop temp

			cap frame change default
			cap frame drop temp
		}
		}

	}
	
* Spain
use spain_ebal.dta, clear 

egen ppvote_p14r = group(voted15_pp p14r_binary), label

save spain_ebal_temp.dta,replace
	
foreach t of varlist treat_full treat_2day treat_1day {

	use spain_ebal_temp.dta, clear 

	 reg `t' /// 
		college churchattend_never unemployed smalltown region_andalucia region_canary region_castilla_lamancha /// 
		region_castile_leon region_catalonia region_valencia region_galicia ///
		region_madrid region_basque
		keep if e(sample)

	ebalance `t' /// 
		college churchattend_never unemployed smalltown region_andalucia region_canary region_castilla_lamancha /// 
		region_castile_leon region_catalonia region_valencia region_galicia ///
		region_madrid region_basque, tar(2) maxiter(50) tolerance(0.03) keep("ebal_es_`t'.dta") replace
	
	svyset [pweight = _webal]
	
foreach var of varlist likelyvote_ciudadanos likelyvote_podemos ///
				likelyvote_psoe likelyvote_pp { 				
		
	local v=substr("`var'",12,14)
		
	frame copy default temp
	frame change temp
	
	eststo sp_`t'_`v'_pp: svy: reg `var' `t'##i.ppvote_p14r
	margins, dydx(`t') over(ppvote_p14r) post

		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Both ppvote and p14r 0"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,5]
		local se = sqrt(v[5,5])
		gen se = `se'
		
		replace type = "Voted PP=0, p14r=1" if _n==2
		replace est = b[1,6] if _n==2
		local se = sqrt(v[6,6])
		replace se = `se' if _n==2

		
		replace type = "Voted PP=1, p14r=0" if _n==3
		replace est = b[1,7] if _n==3
		local se = sqrt(v[7,7])
		replace se = `se' if _n==3


		replace type = "Voted PP=1, p14r=1" if _n==4
		replace est = b[1,8] if _n==4
		local se = sqrt(v[8,8])
		replace se = `se' if _n==4

		keep if _n==1 | _n==2 | _n==3 | _n==4
		keep sample type treat dv est se

		
	frame change results
	frameappend temp
	cap frame drop temp
	frame change default
		
	}
	
	foreach var of varlist vote_PP ///
			combinedproblem_corruption firstproblem_corruption {
			
	frame copy default temp
	frame change temp
	
	svy: logit `var' `t'##i.ppvote_p14r 
	margins, dydx(`t') over(ppvote_p14r) post
		
		mat b=e(b)
		mat v=e(V)
		
		gen sample  = "CIS, Spain"
		gen type = "Both ppvote and p14r 0"
		gen treat = "`t'"
		gen dv = "`var'"
		gen est = b[1,5]
		local se = sqrt(v[5,5])
		gen se = `se'
		
		replace type = "Voted PP=0, p14r=1" if _n==2
		replace est = b[1,6] if _n==2
		local se = sqrt(v[6,6])
		replace se = `se' if _n==2

		
		replace type = "Voted PP=1, p14r=0" if _n==3
		replace est = b[1,7] if _n==3
		local se = sqrt(v[7,7])
		replace se = `se' if _n==3


		replace type = "Voted PP=1, p14r=1" if _n==4
		replace est = b[1,8] if _n==4
		local se = sqrt(v[8,8])
		replace se = `se' if _n==4

		keep if _n==1 | _n==2 | _n==3 | _n==4
		keep sample type treat dv est se
	
		frame change results
		frameappend temp

	cap frame drop temp
	frame change default
		
	}
	
	
	}

frame change results
save ebal_results_extrainteractions.dta, replace
				

esttab treat_1month_v27r_1 ///
treat_1month_v51r_1 treat_1month_v53r_1 using fr_red_double.tex, replace /// 
coeflabels(treat_1month "Treatment" 1.polsop_top40 "High Income Indicator" ///
1.treat_1month#1.top40 "Treatment x High Income") star(+ 0.1 * 0.05) ///
rename (1.treat_1month treat_1month)  drop(_cons) nodepvars ///
nobaselevels mtitles("Inc. Diff" "Inc. Diff" "Hi Tax" "Hi Tax" "Low Tax" "Low Tax") ///
varwidth(30) nonotes b(2) se(2) r2(2) stats(N, fmt(0) labels("Observations")) ///
addnotes("OLS regression estimates calculated with entropy balancing weights." "DV in models 1-2: Gov. should reduce income diff., models 3-4: High income tax too high," "models 5-6: Low income tax too high. + p<0.1, * p<0.05.")

esttab treat_1month_v54r_2  ///
treat_1month_v57r_2 treat_1month_v58r_2 ///
treat_1month_v59r_2 using fr_corr_main.tex, replace ///
coeflabels(treat_1month "Treatment" 1.pr_socialist "Socialist Voter" ///
1.treat_1month#1.pr_socialist "Treatment x Socialist") star(+ 0.1 * 0.05) ///
rename (1.treat_1month treat_1month)  drop(_cons) nodepvars ///
nobaselevels mtitles("Auth" "Auth" "Avoid" "Aviod" "Pol" "Pol" "Pub" "Pub") ///
varwidth(30) nonotes b(2) se(2) r2(2) stats(N, fmt(0) labels("Observations")) ///
addnotes("OLS regression estimates calculated with entropy balancing weights." "DV in models 1-2: Authorities, make people pay takes, models 3-4: Companies avoid paying taxes," "models 5-6: Politicians corrupt, models 7-8: Public officials corrupt. + p<0.1, * p<0.05.")		

esttab sp_treat_2day_ciudadanos sp_treat_2day_ciudadanos_pp sp_treat_2day_podemos ///
sp_treat_2day_podemos_pp sp_treat_2day_psoe sp_treat_2day_psoe_pp sp_treat_2day_pp ///
sp_treat_2day_pp_pp using sp_vote.tex, replace ///
coeflabels(treat_2day "Treatment" 1.voted15_pp "PP Voter" ///
1.treat_2day#1.voted15_pp "Treatment x PP") star(+ 0.1 * 0.05) ///
rename (1.treat_1month treat_1month)  drop(_cons) nodepvars ///
nobaselevels mtitles("Ciud" "Ciud" "Pode" "Pode" "PSOE" "PSOE" "PP" "PP") ///
varwidth(30) nonotes b(2) se(2) r2(2) stats(N, fmt(0) labels("Observations")) ///
addnotes("OLS regression estimates calculated with entropy balancing weights." "DV in models 1-2: Likely to vote Ciudadanos, models 3-4: ...to vote Podemos," "models 5-6: ...to vote PSOE, models 7-8: ...to vote PP. + p<0.1, * p<0.05.")		
*/
